<?php

$this->Html->addCrumb('Home', '/admin');
$this->Html->addCrumb('Configurações', array());
echo $this->Form->create('Configuracoes.Configuracao', array(
    'inputDefaults' => array(
        'div' => 'form-group',
        'wrapInput' => false,
        'class' => 'form-control'
    ),
    'class' => 'well'
));
$configuracoes_grupo = array();
foreach ($configuracoes as $key => $value) {
    $configuracoes_grupo[$value['Configuracao']['grupo']][] = $value;
}


$_ul = array();
$_form = array();

//debug($configuracoes_grupo);
foreach ($configuracoes_grupo as $k => $v) {
    $_ul[$k] = Inflector::humanize($k);
    foreach ($v as $key => $value) {
        $options = array(
            'required' => boolval($value['Configuracao']['required']),
            'type' => $value['Configuracao']['tipo'],
            'value' => (empty($value['Configuracao']['valor']) ? $value['Configuracao']['default'] : $value['Configuracao']['valor']),
            'label' => $value['Configuracao']['nome'],
            'after' => (empty($value['Configuracao']['obs']) ? '' : '<span class="help-block">' . $value['Configuracao']['obs'] . '</span>'),
        );
        switch ($value['Configuracao']['tipo']) {
            case 'editor':
                $options['type'] = 'textarea';
                $_form[$k][] = $this->Midia->redactor('Configuracao.' . $value['Configuracao']['id'], $options);
                break;
            case 'input':
                $options['type'] = 'text';
                $_form[$k][] = $this->Form->input('Configuracao.' . $value['Configuracao']['id'], $options);
                break;
            case 'text':
                $options['readonly'] = true;
                $_form[$k][] = $this->Form->input('Configuracao.' . $value['Configuracao']['id'], $options);
                break;
            case 'radio':
            case 'checkbox':
            case 'select':
                $options['options'] = (empty($value['Configuracao']['options']) ? array() : json_decode($value['Configuracao']['options'], true));
                $_form[$k][] = $this->Form->input('Configuracao.' . $value['Configuracao']['id'], $options);
                break;

            default:
                $_form[$k][] = $this->Form->input('Configuracao.' . $value['Configuracao']['id'], $options);
                break;
        }
    }
}
$_active = 'active';
echo '<ul class="nav nav-tabs">';
foreach ($_ul as $key => $value) {
    echo '<li class="' . $_active . '"><a href="#' . $key . '" data-toggle="tab">' . $value . '</a></li>';
    $_active = '';
}
echo '</ul>';
$_active = 'active';
echo '<div class="tab-content">';
foreach ($_form as $k => $v) {
    echo '<div class="tab-pane ' . $_active . '" id="' . $k . '">';
    foreach ($v as $key => $value) {
        echo $value;
    }
    echo '</div>';
    $_active = '';
}
echo '</div>';

echo $this->Form->button('Salvar', array('class' => 'btn btn-success', 'type' => 'submit', 'icon' => 'fa fa-save'));

echo $this->Form->end();
?>
